package com.webcash.CNBoard.Validation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

import com.webcash.CNBoard.Model.CreateWork;
import com.webcash.CNBoard.Service.CreateWorkService;

@Component
public class CreateWorkValidator implements Validator {
	
	@Autowired
	CreateWorkService createWorkService;

	public boolean supports(Class clazz) {
		
		return (CreateWork.class.isAssignableFrom(clazz));
	}

	public void validate(Object target, Errors errors) {
		CreateWork myWork = (CreateWork)target;
		
		int result = createWorkService.cheackValidation(myWork);
		
		if(result==0){
			errors.rejectValue("id", "not.exist.id", null, "why not");
		}
		if(result == 1){
			errors.rejectValue("upperWork", "not.exist.upperWork");
		}
	}

}
